package com.three.storemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.three.core.entity.*;
import com.three.core.vo.R;
import com.three.storemanage.feign.FrontUserFeign;
import com.three.storemanage.feign.UserFeign;
import com.three.storemanage.mapper.*;
import com.three.storemanage.service.CarWashRecordService;
import com.three.storemanage.vo.CouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @FileName: CarStoreServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/17 19:39
 * @Version: 1.0.0
 */
@Service
public class CarWashRecordServiceImpl implements CarWashRecordService {
    @Autowired
    private CarWashRecordMapper carWashRecordMapper;

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private FrontUserFeign frontUserFeign;

    @Autowired
    private WashCarCouponMapper washCarCouponMapper;

    @Autowired
    private UserBalanceMapper userBalanceMapper;

    @Autowired
    private UserBalanceLogMapper userBalanceLogMapper;

    @Autowired
    private UserIntegralMapper userIntegralMapper;
    @Autowired
    private UserIntegralLogMapper userIntegralLogMapper;

    @Override
    public R<List<CarWashRecord>> selectByStoreId(Integer storeId) {
        List<CarWashRecord> carWashRecords = carWashRecordMapper.selectByStoreId(storeId);
        return new R<>(200,"查询成功",carWashRecords);
    }

    @Override
    public R<List<CarWashRecord>> selectByUserId() {
        R<FrontUser> userInfo = frontUserFeign.getUserInfo();
        int userId = userInfo.getData().getUserId();
        List<CarWashRecord> carWashRecords = carWashRecordMapper.selectByUserId(userId);
        return new R<>(200,"查询成功",carWashRecords);
    }

    @Override
    public R<Integer> countByUserId(Integer userId) {
        Integer count = carWashRecordMapper.countByUserId(userId);
        return new R<>(200,"查询成功",count);
    }

    @Override
    public R<List<CarWashRecord>> selectByUserIdAndDate(Integer userId, Date createDate) {
        List<CarWashRecord> carWashRecords = carWashRecordMapper.selectByUserIdAndDate(userId, createDate);
        return new R<>(200,"查询成功",carWashRecords);
    }

    @Override
    public R<Integer> insertCarWashRecord(CarWashRecord carWashRecord) {
        carWashRecord.setCreateDate(new Date());
        carWashRecord.setStartTime(new Date());
        carWashRecord.setIsNew("1");
        carWashRecord.setWashStatus("0");
        int insert = carWashRecordMapper.insertSelective(carWashRecord);
        Integer stationId = carWashRecord.getStationId();
        int i = stationMapper.updateStationByStationId(stationId);
        if (i > 0){
            return new R<>(200,"添加成功",insert);
        }else {
            return new R<>(500,"添加失败",insert);
        }
    }

    @Override
    public R<List<WashCarCoupon>> selectCouponByCondition(CouponVo couponVo) {
        List<WashCarCoupon> washCarCoupons = washCarCouponMapper.selectCouponByCondition(couponVo);
        return new R<>(200,"查询成功",washCarCoupons);
    }

    @Override
    public R<Integer> updateCarWashRecord(CarWashRecord carWashRecord) {
        carWashRecord.setEndTime(new Date());
        int i = carWashRecordMapper.updateById(carWashRecord);
        Double payMoney = carWashRecord.getPayMoney();
        Double totalAmount = carWashRecord.getTotalAmount();
        Integer orderId = carWashRecord.getOrderId();
        Integer storeId = carWashRecord.getStoreId();
        Integer userId = carWashRecord.getUserId();
        Integer stationId = carWashRecord.getStationId();
        if (totalAmount>payMoney){
            carWashRecordMapper.updateisUsedByOrderId(orderId);
        }else if (totalAmount==payMoney){
            carWashRecordMapper.updateisUsedByOrderId2(orderId);
        }
        int i3 = stationMapper.updateStationByStationId2(stationId);
        int i1 = carWashRecordMapper.updatewashStatusByOrderId(orderId);
        int i2 = userBalanceMapper.updateAmountByUserId(payMoney, storeId, userId);
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(userId);
        userBalanceLog.setStoreId(storeId);
        userBalanceLog.setExpenditure((int) Math.round(payMoney));
        userBalanceLog.setExpenditureTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        int i4 = userBalanceLogMapper.insertSelective(userBalanceLog);
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setStoreId(storeId);
        userIntegral.setCredits((int) Math.round(payMoney));
        userIntegral.setIntegralTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        int i5 = userIntegralMapper.insertSelective(userIntegral);
        UserIntegralLog userIntegralLog = new UserIntegralLog();
        userIntegralLog.setUserId(userId);
        userIntegralLog.setStoreId(storeId);
        userIntegralLog.setArrivalScore((int) Math.round(payMoney));
        userIntegralLog.setRechargeTime(new Date());
        int i6 = userIntegralLogMapper.insertSelective(userIntegralLog);
        return new R<>(200,"修改成功",i);
    }
}
